﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;
using MvcMusicStore.ViewModels;
using LibraryMusicStore;

namespace MvcMusicStore.Controllers
{
    public class ShoppingCartController : Controller
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {
            try
            {
                ViewData["vat"] = 1 + storeDB.VATs.First().Value / 100;
                var cart = ShoppingCart.GetCart(this.HttpContext);

                var viewModel = new ShoppingCartViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartTotal = cart.GetTotal()
                };

                return View(viewModel);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }

        //
        // Get: /store/addtocart/5
        public ActionResult AddToCart(int id)
        {
            try
            {
                //ViewData["vat"] = 1 + storeDB.VATs.First().Value / 100;
                //retrive album
                var album = storeDB.Albums.Single(a => a.AlbumId == id);
                //add to shopping cart
                var cart = ShoppingCart.GetCart(this.HttpContext);

                cart.AddToCart(album);

                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }
        //
        // AJAX: /shoppingcart/changequantity/5
        [HttpPost]
        public ActionResult ChangeQuantity(int id, int quantity)
        {
            try
            {
                var cart = ShoppingCart.GetCart(this.HttpContext);

                cart.ChangeQuantity(id, quantity);

                var result = new ShoppingCartRemoveViewModel
                {
                    CartTotal = cart.GetTotal() * (1 + storeDB.VATs.First().Value / 100),
                    CartCount = cart.GetCount(),
                    ItemCount = quantity,
                    DeleteId = id
                };

                return Json(result);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }
        //
        // AJAX: /ShoppingCart/removefromcart/5
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            try
            {
                //remove the item from the cart
                var cart = ShoppingCart.GetCart(this.HttpContext);

                //get the name of the album to display confirmation
                var albumName = storeDB.Carts
                    .Single(c => c.RecordId == id)
                    .Album.Title;

                //remove from cart. note that for simplicity, we're
                //remove all rather than decrementing the count
                cart.RemoveFromCart(id);

                //var vat = storeDB.VATs.First(v => v.VATId == 1);
                //display the confirmation message
                var result = new ShoppingCartRemoveViewModel
                {
                    Message = Server.HtmlEncode(albumName) +
                        " đã bỏ khỏi giỏ hàng",
                    CartTotal = cart.GetTotal() * (1 + storeDB.VATs.First().Value / 100),
                    CartCount = cart.GetCount(),
                    ItemCount = 0,
                    DeleteId = id
                };

                return Json(result);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }
        //
        //Get: /ShoppingCart/CartSumary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            try
            {
                var cart = ShoppingCart.GetCart(this.HttpContext);

                ViewData["CartCount"] = cart.GetCount();

                return PartialView("CartSummary");
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
            
        }
    }
}
